package com.liqin.nothing.handler;

import com.liqin.nothing.exception.TestException;
import com.liqin.nothing.result.R;
import com.liqin.nothing.result.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    //
    // /**
    //  * 未登录异常
    //  */
    // @ExceptionHandler(NotLoginException.class)
    // public Response<Object> notLoginHandler(NotLoginException e, HttpServletRequest request) {
    //     String requestURI = request.getRequestURI();
    //     log.error("请求地址'{}',未登录异常'{}'", requestURI, e.getLocalizedMessage());
    //     return R.fail(HttpStatus.UNAUTHORIZED, "登录状态失效,请重新登录");
    // }
    //
    // /**
    //  * 权限校验异常
    //  */
    // @ExceptionHandler(NotPermissionException.class)
    // public Response<Object> accessDeniedHandler(NotPermissionException e, HttpServletRequest request) {
    //     String requestURI = request.getRequestURI();
    //     log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getLocalizedMessage());
    //     return R.fail(HttpStatus.FORBIDDEN, "权限不足,无法访问");
    // }

    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response<Object> methodNotSupportedHandler(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return R.fail("不支持的请求方式");
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public Response<Object> notReadableExceptionHandler(HttpMessageNotReadableException e, HttpServletRequest request) {
        log.error("请求地址{} 调用失败 {}", request.getRequestURL(), e.getLocalizedMessage());
        return R.fail("调用接口失败，可能是传参有问题");
    }

    @ExceptionHandler(TestException.class)
    public Response<Object> testExceptionHandler(TestException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}'，我的自定义异常类", requestURI);
        return R.fail(e.getMessage());
    }

    /**
     * 参数错误
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public Response<Object> illegalArgumentHandler(IllegalArgumentException e) {
        log.error("参数错误 {}", e.getLocalizedMessage());
        return R.fail("参数错误");
    }

    // /**
    //  * 自定义异常
    //  */
    // @ExceptionHandler(GlobalException.class)
    // public Response<Object> globalHandler(GlobalException e){
    //     log.error(e.getLocalizedMessage());
    //     return R.fail(e.getLocalizedMessage());
    // }
    //
    // /**
    //  * 登录失败超过最大次数异常
    //  */
    // @ExceptionHandler(LoginFailMaxCountException.class)
    // public Response<Object> loginFailMaxCountHandler(LoginFailMaxCountException e){
    //     log.error(e.getMsg());
    //     return R.fail(e.getMsg());
    // }
    //
    // /**
    //  * 文件分片缺失异常
    //  */
    // @ExceptionHandler(FileShardMissingException.class)
    // public Response<Object> fileShardMissingHandler(FileShardMissingException e){
    //     log.error(e.getMessage());
    //     return R.fail(e.getCode(), e.getMessage(), e);
    // }
    //
    // /**
    //  * 文件md5校验失败
    //  */
    // @ExceptionHandler(FileMD5NotMatchException.class)
    // public Response<Object> fileMD5NotMatchHandler(FileMD5NotMatchException e){
    //     log.error(e.getMessage());
    //     return R.fail(e.getMessage(), e.getMessage());
    // }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public Response<Object> validHandler(BindException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return R.fail(message);
    }


    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public Response<Object> exceptionHandler(Exception e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return R.fail(e.getMessage());
    }
}
